package com.cms.web.delivery;

import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.biz.FlowManagerServiceMgr;
import com.cms.biz.business.DeliveryNoticeServiceMgr;
import com.cms.persistence.contractManager.accreditationContract.PSHTMX;
import com.common.util.DateUtil;
import com.metadata.bizmodel.SYS_MODEL;
import com.persistence.FLOW_TASK_LIST;
import com.persistence.qxgl.beans.SYSUser;
import com.pub.util.EMPJsonUtil;
import com.web.BaseController;

import com.efounder.builder.base.data.EFDataSet;
import com.efounder.builder.base.data.EFRowSet;
import com.efounder.dict.FCTServiceMgr;
import com.efounder.eai.data.JParamObject;
import com.efounder.form.EFFormDataModel;
import com.efounder.form.server.provider.FormModelDataProvider;
import com.efounder.form.server.resolver.util.FormModelDataResolverUtil;

/** 
 * 评审合同控制器
 * 
 * @author ES
 * @version 1.0
 */
@Controller
@RequestMapping(value="/cmsDeliveryNoticeApprovalController")
public class DeliveryNoticeApprovalController extends BaseController{

	@Autowired
	private                            FCTServiceMgr                                   FCTService;

	@Autowired
	private                    FlowManagerServiceMgr                           FlowManagerService;

	@Autowired
	private                 DeliveryNoticeServiceMgr                        deliveryNoticeService;
	
	@Autowired
	private                FormModelDataResolverUtil                            formModelResolver;
		
	@Autowired
	private                    FormModelDataProvider                                formModelUtil;
	
	@Autowired
	public                               HttpSession                                      session;
	
	/**
	 * 生产申请单
	 * @param model
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/deliveryNoticeApproval")
	public String deliveryNoticeApproval(@RequestParam String F_YEAR, String F_MONTH, Model model) throws Exception {	
		EFDataSet       ndDataset = EFDataSet.getInstance();
		EFDataSet      flowNodeDS = null;
		JParamObject           PO = JParamObject.Create();
		SYSUser              user = (SYSUser) session.getAttribute("contextUser");
		String         BILL_WHERE = "";
		
		ndDataset = getKJQJDataSet(F_YEAR);
		flowNodeDS = getFlowNodeDataSet(user);
		
		model.addAttribute("ndDataset", ndDataset);
		model.addAttribute("flowNodeDS", flowNodeDS);
		model.addAttribute("F_KJQJ", F_YEAR + F_MONTH);
		
		if(flowNodeDS.getRowCount() > 0) {
			model.addAttribute("FLOW_NODE", flowNodeDS.getRowSet(0).getString("NODE_ID", ""));
			BILL_WHERE = " and BIZ_DATE = '" + F_YEAR + F_MONTH + "' and ((NODE_TAG = '" + flowNodeDS.getRowSet(0).getString("NODE_ID", "") + "' and RESR_STATUS = 'pending') or "
			           + "(NODE_SRC = '" + flowNodeDS.getRowSet(0).getString("NODE_ID", "") + "' and RESR_STATUS = 'pending' and RESR_IN_CAUSE != 'rollback')) ";
			
			PO.SetValueByParamName("TABLE_NAME", "FLOW_TASK_LIST");
			PO.SetValueByParamName("FLOW_ID", "CCTZ_FLOW");
			PO.SetValueByParamName("FLOW_BM", user.getUSER_ORGID());
			PO.SetValueByParamName("FLOW_USER", user.getUSER_ID());
			PO.SetValueByParamName("TASK_TO_UNIT", user.getUSER_ORGID());
			PO.SetValueByParamName("TASK_TO_USER", user.getUSER_ID());
			PO.SetValueByParamName("BILL_ORDER", " FLOW_TASK_LIST.BIZ_DJBH desc");
			PO.SetValueByParamName("BILL_WHERE", BILL_WHERE);
			
			flowNodeDS = FlowManagerService.loadFlow1(PO);			
			model.addAttribute("dataset", flowNodeDS);
		}
		return "/cms/deliveryNoticeApproval/list";
	}
	
	/**
	 * 生产申请单
	 * @param model
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/deliveryNoticeApproval/list")
	public String deliveryNoticeApprovalList(@RequestParam String F_KJQJ, @RequestParam String FLOW_NODE, Model model) throws Exception {	
		EFDataSet       ndDataset = EFDataSet.getInstance();
		EFDataSet      flowNodeDS = null;
		EFDataSet          flowDS = null;
		JParamObject           PO = JParamObject.Create();
		SYSUser              user = (SYSUser) session.getAttribute("contextUser");
		String         BILL_WHERE = "";
		
		ndDataset = getKJQJDataSet(F_KJQJ.substring(0, 4));
		flowNodeDS = getFlowNodeDataSet(user);

		BILL_WHERE = " and BIZ_DATE = '" + F_KJQJ + "' and ((NODE_TAG = '" + FLOW_NODE + "' and RESR_STATUS = 'pending') or "
		           + "(NODE_SRC = '" + FLOW_NODE + "' and RESR_STATUS = 'pending' and RESR_IN_CAUSE != 'rollback')) ";
		
		PO.SetValueByParamName("TABLE_NAME", "FLOW_TASK_LIST");
		PO.SetValueByParamName("FLOW_ID", "CCTZ_FLOW");
		PO.SetValueByParamName("FLOW_BM", user.getUSER_ORGID());
		PO.SetValueByParamName("FLOW_USER", user.getUSER_ID());
		PO.SetValueByParamName("TASK_TO_UNIT", user.getUSER_ORGID());
		PO.SetValueByParamName("TASK_TO_USER", user.getUSER_ID());
		PO.SetValueByParamName("BILL_ORDER", " FLOW_TASK_LIST.BIZ_DJBH desc");
		PO.SetValueByParamName("BILL_WHERE", BILL_WHERE);
		
		flowDS = FlowManagerService.loadFlow1(PO);	
		
		model.addAttribute("ndDataset", ndDataset);
		model.addAttribute("flowNodeDS", flowNodeDS);
		model.addAttribute("F_KJQJ", F_KJQJ);
		
		model.addAttribute("dataset", flowDS);
		model.addAttribute("FLOW_NODE", FLOW_NODE);
		return "/cms/deliveryNoticeApproval/list";
	}
	
	/**
	 * 获取会计期间
	 * @param F_YEAR
	 * @return
	 */
	private EFDataSet getKJQJDataSet(String F_YEAR) {
		EFDataSet       ndDataset = EFDataSet.getInstance();
		EFRowSet         ndRowset = EFRowSet.getInstance();
		
		for(int i = 1; i <= 12; i++) {
			if(i < 10) {
				ndRowset = EFRowSet.getInstance();
				ndRowset.putString("F_NDBH", F_YEAR + "0" + i);
				ndRowset.putString("F_NDMC", F_YEAR + "0" + i);
				ndDataset.insertRowSet(ndRowset);
			} else {
				ndRowset = EFRowSet.getInstance();
				ndRowset.putString("F_NDBH", F_YEAR + i);
				ndRowset.putString("F_NDMC", F_YEAR + i);
				ndDataset.insertRowSet(ndRowset);
			}
		}
		return ndDataset;
	}
	
	/**
	 * 获取会计期间
	 * @param F_YEAR
	 * @return
	 */
	private EFDataSet getFlowNodeDataSet(SYSUser user) {
		JParamObject          flowPO = JParamObject.Create();
		EFDataSet         flowNodeDS = null;
		
		//获取流程节点
		flowPO.SetValueByParamName("TABLE_NAME", "FLOW_NODE_LIST");
		flowPO.SetValueByParamName("FLOW_ID", "CCTZ_FLOW");
		flowPO.SetValueByParamName("FLOW_BM", user.getUSER_ORGID());
		flowPO.SetValueByParamName("FLOW_USER", user.getUSER_ID());
		flowPO.SetValueByParamName("BILL_ORDER", " NODE_ID");
		
		flowNodeDS = FlowManagerService.loadFlowNode(flowPO);
		return flowNodeDS;
	}
	
	/**
	 * 评审合同添加保存
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/deliveryNoticeApproval/submit", method=RequestMethod.POST)
	public  ModelAndView accreditationContractApprovalSubmit(@RequestBody Map dataMap, Model model) throws Exception {  
		EFRowSet              rowSet = EMPJsonUtil.convertMapToRowSet(dataMap);
		EFRowSet              rowset = EFRowSet.getInstance();
		String              F_CHDATE = "";
		JParamObject              PO = JParamObject.Create();
		JParamObject          flowPO = JParamObject.Create();
		EFDataSet         flowNodeDS = null;
		EFRowSet          nodeRowset = EFRowSet.getInstance();
		SYSUser                 user = (SYSUser) session.getAttribute("contextUser");
		EFFormDataModel    formModel;
		String               OP_NOTE = "";
		
		//获取流程节点
		flowPO.SetValueByParamName("TABLE_NAME", "FLOW_NODE_LIST");
		flowPO.SetValueByParamName("FLOW_ID", "CCTZ_FLOW");
		flowPO.SetValueByParamName("FLOW_BM", user.getUSER_ORGID());
		flowPO.SetValueByParamName("FLOW_USER", user.getUSER_ID());
		flowPO.SetValueByParamName("NODE_ID", "N1");
		flowPO.SetValueByParamName("BILL_ORDER", " NODE_ID");
		
		flowNodeDS = FlowManagerService.loadFlowNode(flowPO);
		nodeRowset = flowNodeDS.getRowSet(0);

		if(nodeRowset == null) {
			return ajaxDoneError("提交失败！原因：<br>【" + user.getUSER_ID() + "-" + user.getUSER_NAME() + "】没有权限提交单据！");
		}
		
		PO.SetValueByParamName(SYS_MODEL.MODEL_ID, "CCTZModel");
		PO.SetValueByParamName(SYS_MODEL.SERVICE_ID, "FormProvider");
		PO.SetValueByParamName("BILL_WHERE", " ####.F_GUID = '" + rowSet.getString("F_GUID", "") + "'");
		formModel = formModelUtil.loadForm(PO);
		EFRowSet headRowset = formModel.getBillDataSet().getRowSet(0);
		headRowset.putString("F_CHDATE", rowSet.getString("F_CHDATE", ""));
		
		PO = JParamObject.Create();
		OP_NOTE = rowSet.getString("OP_NOTE", "");
		if(OP_NOTE == null || OP_NOTE.trim().equals("")) {
			OP_NOTE = rowSet.getString("F_NOTE", "");
		}
		PO.SetValueByParamName(FLOW_TASK_LIST._OP_NOTE, OP_NOTE);
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "01", rowSet.getString("F_HTBH", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "02", dataMap.get("F_HTMC").toString());
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "03", rowSet.getString("F_XMBH", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "04", dataMap.get("F_XMMC").toString());
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "05", rowSet.getString("F_JSQD", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "06", rowSet.getString("F_JSFZR", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "07", rowSet.getString("F_FZRMC", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "08", user.getUSER_ID());
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "09", user.getUSER_NAME());
		PO.SetValueByParamName(FLOW_TASK_LIST._F_DATE + "02", DateUtil.date3String("yyyy-MM-dd"));
		if(dataMap.get("F_WGRQ") != null && !dataMap.get("F_WGRQ").toString().equals("")) {
			PO.SetValueByParamName(FLOW_TASK_LIST._F_DATE + "01", dataMap.get("F_WGRQ").toString());
		}	

		headRowset.putString(EFFormDataModel._FORMEDITSAVETYPE_, "" + EFFormDataModel._FORM_SAVE_ITEM_);
		
		PO.SetValueByParamName(SYS_MODEL.SERVICE_ID, "CMSSubmitFormToFlow");
		PO.SetValueByParamName(SYS_MODEL.MODEL_ID, "CCTZModel");
		
		PO.SetValueByParamName(FLOW_TASK_LIST._FLOW_ID, "CCTZ_FLOW");
		PO.SetValueByParamName(FLOW_TASK_LIST._OP_NODE, nodeRowset.getString("NODE_ID", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._OP_NODE_TYPE, nodeRowset.getString("NODE_TYPE", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._NODE_SRC, nodeRowset.getString("NODE_ID", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._NODE_SRC_NAME, nodeRowset.getString("NODE_NAME", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._OP_BMBH, user.getUSER_ORGID());
		PO.SetValueByParamName(FLOW_TASK_LIST._OP_USER, user.getUSER_ID());
		PO.SetValueByParamName(FLOW_TASK_LIST._OP_USER_NAME, user.getUSER_NAME());
		PO.SetValueByParamName(FLOW_TASK_LIST._RESR_STATUS, "pending");
		PO.SetValueByParamName(FLOW_TASK_LIST._RESR_IN_CAUSE, "submit");
		PO.SetValueByParamName(FLOW_TASK_LIST._RESR_OUT_CAUSE, "submit");
		
		EFFormDataModel formDataModel = formModelResolver.saveForm("CCTZXX", headRowset, PO);
		if(formDataModel.getFormSaveStatus() == EFFormDataModel._FORM_SAVE_FAIL_) {
			return ajaxDoneError("单据提交失败！原因：<br>" + formDataModel.getFormSaveMessage());
		}
		rowset = formDataModel.getBillDataSet().getRowSet(0);
		F_CHDATE = rowset.getObject("F_CHDATE", "").toString();
		model.addAttribute("F_CHDATE", F_CHDATE);
		model.addAttribute("F_DJBH", rowset.getObject("F_DJBH", ""));
		return ajaxDoneSuccess("单据提交成功！"); 
	}
	
	/**
	 * 取回评审合同
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/deliveryNoticeApproval/retake", method=RequestMethod.POST)
	public  ModelAndView productRequisitionsRetake(@RequestBody Map dataMap, Model model) throws Exception {  
		EFRowSet              rowSet = EMPJsonUtil.convertMapToRowSet(dataMap);
		EFRowSet              rowset = EFRowSet.getInstance();
		String              F_CHDATE = "";
		JParamObject              PO = JParamObject.Create();
		JParamObject          flowPO = JParamObject.Create();
		EFDataSet         flowNodeDS = null;
		EFRowSet          nodeRowset = EFRowSet.getInstance();
		SYSUser                 user = (SYSUser) session.getAttribute("contextUser");
		EFFormDataModel    formModel;
		
		//获取流程节点
		flowPO.SetValueByParamName("TABLE_NAME", "FLOW_NODE_LIST");
		flowPO.SetValueByParamName("FLOW_ID", "CCTZ_FLOW");
		flowPO.SetValueByParamName("FLOW_BM", user.getUSER_ORGID());
		flowPO.SetValueByParamName("FLOW_USER", user.getUSER_ID());
		flowPO.SetValueByParamName("NODE_ID", "N1");
		flowPO.SetValueByParamName("BILL_ORDER", " NODE_ID");
		
		flowNodeDS = FlowManagerService.loadFlowNode(flowPO);
		nodeRowset = flowNodeDS.getRowSet(0);

		if(nodeRowset == null) {
			return ajaxDoneError("提交失败！原因：<br>【" + user.getUSER_ID() + "-" + user.getUSER_NAME() + "】没有权限提交单据！");
		}
		
		PO.SetValueByParamName(SYS_MODEL.MODEL_ID, "CCTZModel");
		PO.SetValueByParamName(SYS_MODEL.SERVICE_ID, "FormProvider");
		PO.SetValueByParamName("BILL_WHERE", " ####.F_GUID = '" + rowSet.getString("F_GUID", "") + "'");
		formModel = formModelUtil.loadForm(PO);
		EFRowSet headRowset = formModel.getBillDataSet().getRowSet(0);
		headRowset.putString("F_CHDATE", rowSet.getString("F_CHDATE", ""));
		
		PO = JParamObject.Create();
		PO.SetValueByParamName(FLOW_TASK_LIST._OP_NOTE, rowSet.getString("F_NOTE", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "01", rowSet.getString("F_HTBH", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "02", dataMap.get("F_HTMC").toString());
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "03", rowSet.getString("F_XMBH", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "04", dataMap.get("F_XMMC").toString());
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "05", rowSet.getString("F_JSQD", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "06", rowSet.getString("F_JSFZR", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "07", rowSet.getString("F_FZRMC", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "08", user.getUSER_ID());
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "09", user.getUSER_NAME());
		PO.SetValueByParamName(FLOW_TASK_LIST._F_DATE + "02", DateUtil.date3String("yyyy-MM-dd"));
		if(dataMap.get("F_WGRQ") != null && !dataMap.get("F_WGRQ").toString().equals("")) {
			PO.SetValueByParamName(FLOW_TASK_LIST._F_DATE + "01", dataMap.get("F_WGRQ").toString());
		}	

		headRowset.putString(EFFormDataModel._FORMEDITSAVETYPE_, "" + EFFormDataModel._FORM_SAVE_ITEM_);
		
		PO.SetValueByParamName(SYS_MODEL.SERVICE_ID, "CMSSubmitFormToFlow");
		PO.SetValueByParamName(SYS_MODEL.MODEL_ID, "CCTZModel");
		
		PO.SetValueByParamName(FLOW_TASK_LIST._FLOW_ID, "CCTZ_FLOW");
		PO.SetValueByParamName(FLOW_TASK_LIST._OP_NODE, nodeRowset.getString("NODE_ID", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._OP_NODE_TYPE, nodeRowset.getString("NODE_TYPE", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._NODE_SRC, nodeRowset.getString("NODE_ID", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._NODE_SRC_NAME, nodeRowset.getString("NODE_NAME", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._OP_BMBH, user.getUSER_ORGID());
		PO.SetValueByParamName(FLOW_TASK_LIST._OP_USER, user.getUSER_ID());
		PO.SetValueByParamName(FLOW_TASK_LIST._OP_USER_NAME, user.getUSER_NAME());
		PO.SetValueByParamName(FLOW_TASK_LIST._RESR_STATUS, "pending");
		PO.SetValueByParamName(FLOW_TASK_LIST._RESR_IN_CAUSE, "retake");
		PO.SetValueByParamName(FLOW_TASK_LIST._RESR_OUT_CAUSE, "retake");
		
		EFFormDataModel formDataModel = formModelResolver.saveForm("CCTZXX", headRowset, PO);
		if(formDataModel.getFormSaveStatus() == EFFormDataModel._FORM_SAVE_FAIL_) {
			return ajaxDoneError("单据提交失败！原因：<br>" + formDataModel.getFormSaveMessage());
		}
		rowset = formDataModel.getBillDataSet().getRowSet(0);
		F_CHDATE = rowset.getObject("F_CHDATE", "").toString();
		model.addAttribute("F_CHDATE", F_CHDATE);
		model.addAttribute("F_DJBH", rowset.getObject("F_DJBH", ""));
		return ajaxDoneSuccess("单据提交成功！"); 
	}
	
	/**
	 * 评审合同编辑
	 * @param model
	 * @return
	 */
	@RequestMapping("/deliveryNoticeApproval/edit/{F_GUID}")
	public String productRequisitionsEdit(@PathVariable("F_GUID") String F_GUID, @RequestParam String FLOW_NODE, Model model) {
		JParamObject           PO = JParamObject.Create();
		JParamObject       SYSLPO = JParamObject.Create();
		JParamObject   flowNodePO = JParamObject.Create();
		JParamObject       flowPO = JParamObject.Create();
		EFFormDataModel formModel;
		EFDataSet      flowNodeDS = null;
		EFRowSet       nodeRowset = null;
		EFDataSet     flowDataSet = EFDataSet.getInstance();
		EFRowSet       flowRowset = EFRowSet.getInstance();
		EFRowSet   nextFlowRowset = EFRowSet.getInstance();
		SYSUser              user = (SYSUser) session.getAttribute("contextUser");
		String         BILL_WHERE = "";
		double               scsl = 0.0;
		double               ccsl = 0.0;
		EFRowSet   cctzHeadRowSet = EFRowSet.getInstance();
		EFDataSet cctzItemDataSet = null;

		//获取流程节点
		flowNodePO.SetValueByParamName("TABLE_NAME", "FLOW_NODE_LIST");
		flowNodePO.SetValueByParamName("FLOW_ID", "CCTZ_FLOW");
		flowNodePO.SetValueByParamName("FLOW_BM", user.getUSER_ORGID());
		flowNodePO.SetValueByParamName("FLOW_USER", user.getUSER_ID());
		flowNodePO.SetValueByParamName("NODE_ID", FLOW_NODE);
		flowNodePO.SetValueByParamName("BILL_ORDER", " NODE_ID");
		
		flowNodeDS = FlowManagerService.loadFlowNode(flowNodePO);
		
		try {
			PO.SetValueByParamName("F_GUID", F_GUID);
			cctzHeadRowSet = deliveryNoticeService.loadEditCCTZForm(PO);
			
			cctzItemDataSet = cctzHeadRowSet.getDataSet("CCTZMX");
//			formModel = formModelUtil.loadForm(PO);
//			EFDataSet itemCTN = metaDataServiceMgr.getSYS_MDL_CTN("CCTZModel", SYS_MDL_CTN._BIZ_CTN_TYPE_JIDS_);
//			EFRowSet rowset = formModel.getBillDataSet().getRowSet(0);
//			EFDataSet dataset = rowset.getDataSet(itemCTN.getRowSet(0).getString("CTN_FCT1", ""));

			//计算剩余数量
//			EFDataSet syslDataset = null;
//			EFRowSet  syslRowSet = null;
//			BILL_WHERE = " F_KJQJ = '" + rowset.getString("SCTZ_KJQJ", "") + "' and F_DJBH = '" + rowset.getString("SCTZ_DJBH", "") + "' and "
//			           + " F_GUID = '" + rowset.getString("SCTZ_GUID", "") + "'";
//			SYSLPO.SetValueByParamName("tableName", "SCSYSL_V");
//			SYSLPO.SetValueByParamName("BILL_ORDER", " F_FLBH desc ");
//			SYSLPO.SetValueByParamName("BILL_WHERE", BILL_WHERE);
//			SYSLPO.SetValueByParamName("loadMeataData", "0");
//			syslDataset = FCTService.getFCTRows(SYSLPO);
//			if(syslDataset != null && syslDataset.getRowCount() > 0) {
//				syslDataset.setPrimeKey(new String[]{"F_FLBH"});
//				syslDataset.buildPrimeKeyIndex();
//				for(int i = 0; i < dataset.getRowCount(); i++) {
//					syslRowSet = (EFRowSet) syslDataset.getRowSet(dataset.getRowSet(i).getString("F_FLBH", ""));
//					if(syslRowSet != null) {
//						scsl = syslRowSet.getNumber("F_SCSL", 0.0).doubleValue();
//						ccsl = syslRowSet.getNumber("F_CCSL", 0.0).doubleValue();
//						BigDecimal sysl = new BigDecimal(scsl - ccsl);
//						sysl.setScale(2, BigDecimal.ROUND_HALF_UP);
//						dataset.getRowSet(i).putObject("F_SYSL", scsl - ccsl);
//					}
//				}
//			} else {
//				for(int i = 0; i < dataset.getRowCount(); i++) {
//					dataset.getRowSet(i).putObject("F_SYSL", dataset.getRowSet(i).getObject("F_SCSL", "0"));
//				}
//			}
			
			if(flowNodeDS != null && flowNodeDS.getRowCount() > 0) {
				nodeRowset = flowNodeDS.getRowSet(0);
				//获取流程信息
				BILL_WHERE = " BIZ_DATE = '" + cctzHeadRowSet.getString("F_KJQJ", "") + "' and BIZ_DJBH = '" + cctzHeadRowSet.getString("F_DJBH", "") + "' and "
				           + " FLOW_ID = 'CCTZ_FLOW' and NODE_TAG = '" + nodeRowset.getString("NODE_ID", "") + "'";
				flowPO.SetValueByParamName("tableName", "FLOW_TASK_LIST");
				flowPO.SetValueByParamName("BILL_ORDER", " (OP_ID + 0) desc ");
				flowPO.SetValueByParamName("BILL_WHERE", BILL_WHERE);
				flowDataSet = FCTService.getFCTRows(flowPO);
				if(flowDataSet.getRowCount() > 0) {
					flowRowset = flowDataSet.getRowSet(0);
					//modify by prayer 2014-11-03 因为编辑的时候打开有问题，留着也没用，清空掉了
					for(int i = 1; i < 10; i++) {
						flowRowset.putString("F_CHAR0" + i, "");
					}
					for(int i = 10; i <= 20; i++) {
						flowRowset.putString("F_CHAR" + i, "");
					}
					flowRowset.putString("OP_NOTE", "");
					model.addAttribute("flowObject", flowDataSet);
				}
				
				//获取下一个流程信息
				BILL_WHERE = " BIZ_DATE = '" + cctzHeadRowSet.getString("F_KJQJ", "") + "' and BIZ_DJBH = '" + cctzHeadRowSet.getString("F_DJBH", "") + "' and "
				           + " FLOW_ID = 'CCTZ_FLOW' and POP_ID = '" + flowRowset.getString("OP_ID", "") + "'";
				flowPO.SetValueByParamName("tableName", "FLOW_TASK_LIST");
				flowPO.SetValueByParamName("BILL_ORDER", " (OP_ID + 0) desc ");
				flowPO.SetValueByParamName("BILL_WHERE", BILL_WHERE);
				flowDataSet = FCTService.getFCTRows(flowPO);
				if(flowDataSet.getRowCount() > 0) {
					nextFlowRowset = flowDataSet.getRowSet(0);
				}
			}

			//获取流程节点
			if(nodeRowset != null) {
				cctzHeadRowSet.putString("FLOW_NODE_NAME", nodeRowset.getString("NODE_NAME", ""));
				//如果当前流程状态为代办，则可以提交和编辑
				if(flowRowset.getString("RESR_STATUS", "").equals("pending")) {
					if(nodeRowset.getString("NODE_TYPE", "").equals("start")) {
						model.addAttribute("canRollback", "0");
					} else {
						model.addAttribute("canRollback", "1");
					}
					model.addAttribute("canEdit", "0");
					model.addAttribute("canSubmit", "1");
					model.addAttribute("canRetrieve", "0");					
				}
				//如果已经处理过，则判断下一个流程，下一个流程如果为代办，则可以取回，否则不能取回
				else {
					if(nextFlowRowset.getString("RESR_STATUS", "").equals("pending")) {
						model.addAttribute("canEdit", "0");
						model.addAttribute("canSubmit", "0");
						if(flowRowset.getString("RESR_OUT_CAUSE", "").equals("rollback")) {
							model.addAttribute("canRetrieve", "0");
						} else {
							model.addAttribute("canRetrieve", "1");
						}
						model.addAttribute("canRollback", "0");
					} else {
						model.addAttribute("canEdit", "0");
						model.addAttribute("canSubmit", "0");
						model.addAttribute("canRetrieve", "0");
						model.addAttribute("canRollback", "0");
					}					
				}
				model.addAttribute("nodeRowSet", nodeRowset);
			} else {
				cctzHeadRowSet.putString("FLOW_NODE_NAME", "提交任务");
				model.addAttribute("canEdit", "0");
				model.addAttribute("canSubmit", "0");
				model.addAttribute("canRetrieve", "0");
				model.addAttribute("canRollback", "0");
				model.addAttribute("nodeRowSet", null);
			}
			model.addAttribute("FLOW_NODE", FLOW_NODE);
			model.addAttribute("dictObject", cctzHeadRowSet);
			model.addAttribute("dataset", cctzItemDataSet);			
			model.addAttribute("flowNodeObject", nodeRowset);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/cms/deliveryNoticeApproval/form";
	}	
	
	/**
	 * 取回评审合同
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/deliveryNoticeApproval/rollback", method=RequestMethod.POST)
	public  ModelAndView accreditationContractApprovalRollback(@RequestBody Map dataMap, Model model) throws Exception {  
		EFRowSet              rowSet = EMPJsonUtil.convertMapToRowSet(dataMap);
		EFDataSet        itemDataSet = rowSet.getDataSet(PSHTMX.class.getName());
		EFRowSet              rowset = EFRowSet.getInstance();
		String              F_CHDATE = "";
		JParamObject              PO = JParamObject.Create();
		JParamObject          flowPO = JParamObject.Create();
		EFDataSet         flowNodeDS = null;
		EFRowSet          nodeRowset = EFRowSet.getInstance();
		SYSUser                 user = (SYSUser) session.getAttribute("contextUser");
		String            BILL_WHERE = " OP_ID = '" + rowSet.getString("OP_ID", "") + "'";

		PO.SetValueByParamName("tableName", "FLOW_TASK_LIST");
		PO.SetValueByParamName("BILL_WHERE", BILL_WHERE);
		EFRowSet          flowRowSet = FCTService.getFCTRow(PO);

		
		//获取流程节点
		flowPO.SetValueByParamName("TABLE_NAME", "FLOW_NODE_LIST");
		flowPO.SetValueByParamName("FLOW_ID", "PSHT_EDIT_FLOW");
		flowPO.SetValueByParamName("FLOW_BM", user.getUSER_ORGID());
		flowPO.SetValueByParamName("FLOW_USER", user.getUSER_ID());
		flowPO.SetValueByParamName("NODE_ID", flowRowSet.getString("NODE_TAG", ""));
		flowPO.SetValueByParamName("BILL_ORDER", " NODE_ID");
		
		flowNodeDS = FlowManagerService.loadFlowNode(flowPO);
		nodeRowset = flowNodeDS.getRowSet(0);

		if(nodeRowset == null) {
			return ajaxDoneError("提交失败！原因：<br>【" + user.getUSER_ID() + "-" + user.getUSER_NAME() + "】没有权限提交单据！");
		}

		itemDataSet.setTableName("PSHTMX");
		rowset.setDataMap(rowSet.getDataMap());
		rowset.remove(PSHTMX.class.getName());
		rowset.putString("F_DJZT", "2");
		rowset.putString(EFFormDataModel._FORMEDITSAVETYPE_, "" + EFFormDataModel._FORM_SAVE_HEAD_);
		
		PO.SetValueByParamName(SYS_MODEL.SERVICE_ID, "RollbackApprovalPSHTRToFlow");
		PO.SetValueByParamName(SYS_MODEL.MODEL_ID, "PSHTModel");
		
		PO.SetValueByParamName(FLOW_TASK_LIST._OP_ID, flowRowSet.getString("OP_ID", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._FLOW_ID, "PSHT_EDIT_FLOW");
		PO.SetValueByParamName(FLOW_TASK_LIST._OP_NODE, nodeRowset.getString("NODE_ID", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._OP_NODE_TYPE, nodeRowset.getString("NODE_TYPE", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._NODE_SRC, nodeRowset.getString("NODE_ID", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._NODE_SRC_NAME, nodeRowset.getString("NODE_NAME", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._OP_BMBH, user.getUSER_ORGID());
		PO.SetValueByParamName(FLOW_TASK_LIST._OP_USER, user.getUSER_ID());
		PO.SetValueByParamName(FLOW_TASK_LIST._TASK_TO_UNIT, user.getUSER_ORGID());
		PO.SetValueByParamName(FLOW_TASK_LIST._OP_USER_NAME, user.getUSER_NAME());
		PO.SetValueByParamName(FLOW_TASK_LIST._RESR_OUT_CAUSE, "rollback");
		PO.SetValueByParamName(FLOW_TASK_LIST._RESR_STATUS, "pending");
		PO.SetValueByParamName(FLOW_TASK_LIST._RESR_IN_CAUSE, "rollback");
		PO.SetValueByParamName(FLOW_TASK_LIST._OP_NOTE, dataMap.get("OP_NOTE").toString());
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "01", rowset.getString("F_KHBH", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "02", rowset.getString("F_KHMC", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "03", rowset.getString("F_XMBH", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "04", rowset.getString("F_XMMC", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "05", rowset.getString("F_BZ", ""));
		PO.SetValueByParamName(FLOW_TASK_LIST._F_CHAR + "06", rowset.getString("F_FSCS", ""));
		
		EFFormDataModel formDataModel = formModelResolver.saveForm("PSHTXX", rowset, PO);
		if(formDataModel.getFormSaveStatus() == EFFormDataModel._FORM_SAVE_ITEMS_ALL_) {
			return ajaxDoneError("单据提交失败！原因：<br>" + formDataModel.getFormSaveMessage());
		}
		rowset = formDataModel.getBillDataSet().getRowSet(0);
		F_CHDATE = rowset.getObject("F_CHDATE", "").toString();
		model.addAttribute("F_CHDATE", F_CHDATE);
		model.addAttribute("F_DJBH", rowset.getObject("F_DJBH", ""));
		model.addAttribute("OP_ID", PO.GetValueByParamName(FLOW_TASK_LIST._OP_ID, ""));
		return ajaxDoneSuccess("单据提交成功！"); 
	}
}
